﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace BYManageInfoSystem
{
    public partial class Frm_TargetNation : Form
    {
        private DGBY.BLL.MDCountryTab m_bllMDCountryTab = new DGBY.BLL.MDCountryTab();
        private DataSet m_ds = new DataSet();

        private bool _IsInsert = false;
        public bool bIsInsert
        {
            get
            {
                return _IsInsert;
            }
            set
            {
                _IsInsert = value;

                buttonAddPort.Enabled = !value;
                buttonDelPort.Enabled = !value;
            }
        }
        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;
        }
        public Frm_TargetNation()
        {
            InitializeComponent();
            InitUltraWinGridSetting();
        }

        private void ShowAllRecordInDataGridView()
        {
            ultraGrid1.DataSource = null;
            m_ds = m_bllMDCountryTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
            //this.ultraGrid1.DisplayLayout.ValueLists.Add("国家名称");
            //for (int i = 0; i < m_ds.Tables[0].Rows.Count; i++)
            //{
            //    this.ultraGrid1.DisplayLayout.ValueLists["国家名称"].ValueListItems.Add(m_ds.Tables[0].Rows[i].ItemArray.GetValue(1));
                

            //}
        }

        private void buttonShowAllRecordPort_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void buttonAddPort_Click(object sender, EventArgs e)
        {
            if (!bIsInsert)
            {
                ultraGrid1.DisplayLayout.Bands[0].AddNew();
            }
        }

        private void buttonModifyPort_Click(object sender, EventArgs e)
        {
            ultraGrid1.UpdateData();
            return;
        }

        private void buttonDelPort_Click(object sender, EventArgs e)
        {

            if (ultraGrid1.ActiveRow != null)
            {
                ultraGrid1.ActiveRow.Delete(false);
            }
            return;
            
        }

        private void ultraGrid1_AfterRowInsert(object sender, Infragistics.Win.UltraWinGrid.RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[0].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[0].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[0].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            bIsInsert = true;
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow mFila;
                mFila = ultraGrid1.Rows[e.Row.Index];
                //if (validarDatosGrid(mFila))
                {
                    DGBY.Model.MDCountryTab modelMDCountryTab = new DGBY.Model.MDCountryTab();
                    modelMDCountryTab.MDCountryName = mFila.Cells[1].Value.ToString();


                    if (bIsInsert)
                    {
                        int nResult = m_bllMDCountryTab.Add(modelMDCountryTab);
                        ultraGrid1.Rows[e.Row.Index].Cells[0].Value = nResult;
                        ultraGrid1.Rows[e.Row.Index].Cells[0].Activation = Activation.ActivateOnly;

                        bIsInsert = false;

                        
                    }
                    else
                    {
                        int nId = Convert.ToInt32(mFila.Cells[0].Value.ToString(), 10);
                        modelMDCountryTab.MDCountryTabID = nId;
                        m_bllMDCountryTab.Update(modelMDCountryTab);
                        
                    }

                }
                //else
                //{
                //    e.Cancel = true;
                //}
            }
            catch (Exception ex)
            {
                e.Cancel = true;
            }
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
                ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false, false);
                e.Handled = true;
                ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
            }
            //else if (e.KeyCode == Keys.Tab)
            //{
            //    if (!bIsInsert && this.ultraGrid1.ActiveCell.Row.Index == this.ultraGrid1.Rows.Count - 1 && this.ultraGrid1.ActiveCell.Column.Index == this.ultraGrid1.DisplayLayout.Bands[0].Columns.Count - 1)
            //    {
            //        if (!bIsInsert)
            //        {
            //            ultraGrid1.DisplayLayout.Bands[0].AddNew();
            //            e.Handled = true;
            //        }
            //    }
            //}
            //else if (e.KeyCode == Keys.S && e.Control)
            //{
            //    ultraGrid1.UpdateData();
            //}
        }

        private void ultraGrid1_BeforeRowCancelUpdate(object sender, CancelableRowEventArgs e)
        {
            bIsInsert = false;
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!bIsInsert)
            {

                DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    e.Cancel = true;
                    return;
                }

                {
                    try
                    {
                        int nId = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                        m_bllMDCountryTab.Delete(nId);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("没有要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        //MessageBox.Show(Ocurrio un error al borrar los datos de la fila   + ex.Message, Borrar Pantalla, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        e.Cancel = true;
                    }
                }

            }
            else
            {
                MessageBox.Show("请先完成插入操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e)
        {
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["国家名称"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDown;
            //ValueList citiesValueList = new ValueList();
            //citiesValueList.ValueListItems.Add("songbo");
            //citiesValueList.ValueListItems.Add("ddd");
            //e.Layout.Bands[0].Columns["国家名称"].ValueList = citiesValueList;
        }

        private void ultraGrid1_CellChange(object sender, CellEventArgs e)
        {
           //string fieldType = e.Cell.Value.ToString();
           // fieldType = e.Cell.Text;

        }

        private void ultraGrid1_AfterCellUpdate(object sender, CellEventArgs e)
        {
            string fieldType = e.Cell.Value.ToString();
            fieldType = e.Cell.Text;
        }
    }
}
